//
//  RichTextViewViewController.m
//  AFNetworking
//
//  Created by admin on 2020/4/27.
//

#import "RichTextViewViewController.h"
#import <RZRichTextView/RZRichTextView.h>
#import <BlocksKit/BlocksKit.h>
@interface RichTextViewViewController ()

@property(strong, nonatomic)UIButton* commitBtn;

@property(strong, nonatomic)RZRichTextView *richTextView;
@end

@implementation RichTextViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)configUI{
    self.title = @"富文本编辑";
    self.view.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.commitBtn];
    [self.view addSubview:self.richTextView];
}
-(UIButton *)commitBtn{
    
    if (!_commitBtn) {
        _commitBtn = [UIButton new];
        [_commitBtn setTitle:@"查看html内容" forState:UIControlStateNormal];
        [_commitBtn setBackgroundColor:COLORDANGER];
        [_commitBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            
        }];
    }
    return _commitBtn;
}
-(RZRichTextView *)richTextView{
    if (!_richTextView) {
        _richTextView = [RZRichTextView new];
        _richTextView.rz_attributeItems = [self loadAttributeItemDatas];
        _richTextView.ug_placeholderStr = @"点击编辑商品详情";
        [_richTextView ug_borderColor:COLORINFO width:1];
        UG_WEAKSELF
        [_richTextView setRz_shouldInserImage:^UIImage * _Nullable(UIImage * _Nullable image) {
            [weakSelf.view ug_starloading];
            [weakSelf bk_performBlock:^(id obj) {
                [weakSelf.view ug_stoploading];
                [weakSelf changeimage:image tourl:@""];
            }  afterDelay:5];
            return image;
        }];
        [_richTextView setRz_didChangedText:^(RZRichTextView * _Nonnull textView) {
            NSLog(@"textView.attributedText:%@",textView.attributedText);
        }];
    }
    return _richTextView;
}
-(void)changeimage:(UIImage*)image tourl:(NSString*)url{
    
}
- (NSMutableArray*)loadAttributeItemDatas {
    NSMutableArray *temdarr = [NSMutableArray new];
    // 图片
    RZRichTextAttributeItem *item1 = [RZRichTextAttributeItem initWithType:RZRichTextAttributeTypeAttachment defaultImage:k_rz_richImage(@"rz_image") highlightImage:nil highlight:NO];
    [temdarr addObject:item1];
    
    // 撤销
    RZRichTextAttributeItem *item2 = [RZRichTextAttributeItem initWithType:RZRichTextAttributeTypeRevoke defaultImage:k_rz_richImage(@"rz_revoke_d") highlightImage:k_rz_richImage(@"rz_revoke_h") highlight:NO];
    [temdarr addObject:item2];
    
    // 恢复
    RZRichTextAttributeItem *item3 = [RZRichTextAttributeItem initWithType:RZRichTextAttributeTypeRestore defaultImage:k_rz_richImage(@"rz_restore_d") highlightImage:k_rz_richImage(@"rz_restore_h") highlight:NO];
    [temdarr addObject:item3];
    
    
    // 链接
    RZRichTextAttributeItem *item18 = [RZRichTextAttributeItem initWithType:RZRichTextAttributeTypeURL defaultImage:k_rz_richImage(@"rz_url_d") highlightImage:nil highlight:NO];
    [temdarr addObject:item18];
    return temdarr;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_richTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(300);
    }];
    
    [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.richTextView.mas_bottom).mas_offset(KPAND_DEF);;
        make.left.mas_equalTo(self.view).mas_offset(KPAND_DEF);
        make.right.mas_equalTo(self.view).mas_offset(-KPAND_DEF);
        make.height.mas_equalTo(44);
    }];
}

@end
